<template>
  <div>
    <!--    查询条件-->
    <div style="margin-bottom: 5px;">
      <el-input v-model="name" placeholder="请输入类目名称" suffix-icon="el-icon-search" style="width: 200px;"
                @keyup.enter.native="loadPost"></el-input>

      <el-button style="margin-left: 5px;" type="primary" @click="loadPost">查询</el-button>
      <el-button style="margin-left: 5px;" type="success" @click="resetParam">重置</el-button>

      <el-button style="margin-left: 5px;" type="primary" @click="add">新增</el-button>


    </div>
    <el-table :data="tableData"
              :header-cell-style="{ background:'#f2f5fc',color:'#555555' }"
              border
    >
      <el-table-column prop="id" label="id" width="60">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="bookname" label="书名" width="100" >
<!--        :formatter="formatBooks"-->
      </el-table-column>
      <el-table-column prop="money" label="价格" width="60" :formatter="lormatMoner">
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="150">
      </el-table-column>
      <el-table-column prop="address" label="地址" width="500">
      </el-table-column>

      <el-table-column prop="operate" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除?"
              @confirm="del(scope.row.id)"
              style="margin-left: 5px;"
          >
            <el-button slot="reference" size="mini" type="danger" >删除</el-button>
          </el-popconfirm>
        </template>

      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        :title="this.flage === 'modflage'?'编辑':'新增'"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>

      <!--      新增-->
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-col :span="15">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="书名" prop="bookname">
          <el-col :span="15">
            <el-input v-model="form.bookname"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="价格" prop="money">
          <el-col :span="15">
            <el-input v-model="form.money"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-col :span="15">
            <el-input v-model="form.phone"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-col :span="15">
            <el-input v-model="form.address"></el-input>
          </el-col>
        </el-form-item>


      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>


  </div>
</template>

<script>
export default {
  name: "OrderManage",
  data() {

    flage:''




    return {
      booksData:[],
      tableData:[],
      pageSize:10,
      pageNum:1,
      total:0,
      name:'',



      centerDialogVisible:false,
      form:{
        id:'',
        name:'',
        bookname:'',
        bookcode:'',
        money:'',
        phone:'',
        address:'',

      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        bookname: [
          { required: true, message: '请输入书名', trigger: 'blur' },
        ],
        money: [
          { required: true, message: '请输入价格', trigger: 'blur' },
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' },
        ],


      }
    }
  },
  methods:{
    //将图书表中的价格传到订单中
    lormatMoner(row){
      let temp = this.booksData.find(item=>{
        //将图书表中的价格传到订单中,以图书编码作为唯一标识
        return item.bookcode == row.money
      })
      return temp && temp.price
    },
    //将图书表里的书名传到用户订单表中
    // formatBooks(row){
    //   let temp = this.bookData.find(itm=>{
    //     return itm.id == row.order
    //   })
    //   return temp && temp.bookname
    // },
    resetForm() {
      this.$refs.form.resetFields();
    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/orders/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    doSave(){
      this.$axios.post(this.$httpUrl+'/orders/save',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible = false
          this.loadPost()
          this.resetForm()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.flage === 'modflage'){
            this.doMod();
          }else{
            this.doSave();
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //删除
    del(id){
      console.log(id)

      this.$axios.get(this.$httpUrl+'/orders/del?id='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){

          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })


    },
    //修改
    mod(row){
      this.flage= 'modflage'
      console.log(row)

      this.centerDialogVisible = true
      this.$nextTick(()=>{
        //赋值到表单

        this.form.id = row.id
        this.form.name = row.name
        this.form.bookname = row.bookname
        this.form.money = row.money
        this.form.phone = row.phone
        this.form.address = row.address

      })

    },
    //添加
    add(){
      this.flage = 'addflage'
      this.centerDialogVisible = true
      this.$nextTick(()=>{
        this.resetForm()
      })



    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },

    resetParam(){
      this.name=''

    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/orders/listPage',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        param:{
          name:this.name,


        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else ('获取数据失败')

      })
    },
    //引入图书表中数据
    loadBook(){
      this.$axios.get(this.$httpUrl+'/books/list').then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.booksData=res.data
        }else ('获取数据失败')

      })
    }
  },
  beforeMount() {
    this.loadBook()
    this.loadPost()

  }
}
</script>

<style scoped>

</style>